package fastwave.cloud.demo.fastwave.biz.publisher.service.impl;

import fastwave.cloud.demo.fastwave.biz.publisher.domain.Trans;
import fastwave.cloud.demo.fastwave.biz.publisher.mapper.TransMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fastwave.cloud.demo.fastwave.biz.publisher.domain.Account;
import fastwave.cloud.demo.fastwave.biz.publisher.mapper.AccountMapper;
import fastwave.cloud.demo.fastwave.biz.publisher.service.AccountService;
/**
* <p>Title: AccountServiceImpl</p>
* @author: dwLiu
* @date: 2020/4/11 21:19
*/
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService{

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private TransMapper transMapper;
    @Override
    public String transfer(Integer id1, Integer id2, Float amount) {
        try {
            //扣减
            Account account1 = accountMapper.selectById(id1);
            account1.setBalance(account1.getBalance()-amount);
            accountMapper.updateById(account1);
            //增加
            Account account2 = accountMapper.selectById(id2);
            account1.setBalance(account2.getBalance()+amount);
            accountMapper.updateById(account2);
            //流水日志
            Trans trans =new Trans();
            trans.setId(UUID.randomUUID().toString());
            trans.setStatus(1);
            transMapper.insert(trans);
            return "转账成功";
        } catch (Exception e) {
            return "转账失败";
        }
    }
}
